package org.example.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.commonapi.domain.Task;
import org.example.commonapi.dto.TaskFinish;
import org.example.commonapi.dto.TaskInfoDto;
import org.example.commonapi.util.TimeUtil;
import org.example.course.service.TaskService;
import org.example.commonapi.mapper.TaskMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author DELL
* @description 针对表【task(任务)】的数据库操作Service实现
* @createDate 2024-06-03 15:12:14
*/
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task>
    implements TaskService{

    @Resource
    private TaskMapper taskMapper;

    /**
     * 查询课程任务列表
     * @param courseId
     * @param name
     * @param type
     * @return
     */
    @Override
    public List<TaskInfoDto> courseTaskList(Long courseId, String name, Integer type) {
        return taskMapper.courseTaskList(courseId,name, type);
    }

    /**
     * 添加任务
     * @param task
     */
    @Override
    public void insertTask(Task task) {
        task.setCreateTime(LocalDateTime.now());
        taskMapper.insert(task);
    }

    /**
     * 获取到进行中任务和结束任务
     * @param courseId
     * @return
     */
    @Override
    public List<List<Task>> courseDoingTaskList(Long courseId) {
//        获取所有数据
        LambdaQueryWrapper<Task> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getCourseId,courseId);
        List<Task> allTask=taskMapper.selectList(queryWrapper);
//        获取进行中任务和结束任务
        List<List<Task>> list =new ArrayList<>();
        List<Task> endTaskList= new ArrayList<>();
        List<Task> doingTaskList= new ArrayList<>();
        for(Task task:allTask){
//            说明结束
            if(TimeUtil.compareTime(LocalDateTime.now(),task.getEndTime()))
                endTaskList.add(task);
            else
                doingTaskList.add(task);
        }
//        0
        list.add(doingTaskList);
//        1
        list.add(endTaskList);
        return list;
    }

    @Override
    public List<TaskFinish> listTaskDetail(Long courseId) {
        return taskMapper.listTaskDetail(courseId);
    }
}




